#============================================
# #知识点
# 当你知道锁的使用抢锁必须要释放锁，其实你在操作锁的时候也极其容易产生死锁现象(整
# 个程序卡死阻塞)
# from threading import RLock, Thread,Lock
# import time
# mutexA = Lock()
# mutexB = Lock()
# class MyThead(Thread):
#     def run(self):
#         self.func1()
#         self.func2()
#     def func1(self):
#         mutexA. acquire()
#         print('%s抢到A锁'%self. name) # 获取当前线程名
#         mutexB . acquire( )
#         print('%s抢到B锁'%self. name)
#         mutexB . release( )
#         mutexA. release()
#     def func2(self) :
#         mutexB . acquire( ) 
#         print( '%s抢到B锁'%self. name)
#         time.sleep(2)
#         mutexA. acquire()
#         print('%s抢到A锁'%self. name) # 获取当前线程名
#         mutexA. release( )
#         mutexB . release( )
# if __name__ == "__main__":
#     for i in range(10):
#         t = MyThead()
        # t. start()
#============================================
# 递归锁
# 递归锁的特点
# 可以被连续的acquire和release
# 但是只能被第一个抢到这把锁执行上述操作
# 它的内部有-个计数器每acquire- -次计数加一每realse- 次计数减一
# 只要计数不为0那么其他人都无法抢到该锁
# from threading import Thread,Lock,RLock
# import time
# mutexA = Lock()
# mutexB = Lock()
# mutexB = mutexA = RLock()
# class MyThead(Thread):
#     def run(self):
#         self.func1()
#         self.func2()
#     def func1(self):
#         mutexA. acquire()
#         print('%s抢到A锁'%self. name) # 获取当前线程名
#         mutexB . acquire( )
#         print('%s抢到B锁'%self. name)
#         mutexB . release( )
#         mutexA. release()
#     def func2(self) :
#         mutexB . acquire( ) 
#         print( '%s抢到B锁'%self. name)
#         time.sleep(2)
#         mutexA. acquire()
#         print('%s抢到A锁'%self. name) # 获取当前线程名
#         mutexA. release( )
#         mutexB . release( )
# if __name__ == "__main__":
#     for i in range(10):
#         t = MyThead()
#         t. start()
#============================================
# 信号量(了解)
# 信号量在不同的阶段可能对应不同的技术点
# 在并发编程中信号量指的是锁!!
# 如果我们将互斥锁比喻成一个厕所的话
# 那么信号量就相当于多个厕所
from threading import Thread,Semaphore
import time
import random
mutex = Semaphore(3)
def task(i):
    mutex.acquire()
    print(f"伞兵{i}号准备就绪")
    time.sleep(random.randint(1,2))
    print("=========")
    mutex.release()

if __name__ == "__main__":
    for i in range(9):
        x = Thread(target=task,args=(i,))
        x.start()
#===========================================
